<template>
  <div class="app-container">
    <div style="float: left;margin-bottom: 20px">
      <el-input v-model="listQuery.tableName" clearable placeholder="请输入表名" style="width: 200px;"  @keyup.enter.native="handleFilter" />
      <el-button  type="primary" plain @click="handleFilter" >搜索</el-button>
    </div>
    <el-table
      :data="list"
      border
      style="width: 100%">
      <el-table-column
        label="表名"
        align="center"
        width="180">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.tableName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">生成页面</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>
    <el-dialog title="生成页面" :visible.sync="dialogFormVisible" fullscreen>
      <el-form>
          <h2>搜索字段</h2>
          <el-form-item
            v-for="(domain, index) in formData.searchData"
            :label="'搜索字段' + index"
            label-width="80px"
          >
            <el-col :span="8">
              <el-select v-model="domain.name" clearable placeholder="请选择字段">
                <el-option
                  v-for="item in tableData"
                  :label="item.columnName"
                  :value="item.columnName">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-select v-model="domain.type" clearable placeholder="请选择类型">
                <el-option
                  v-for="item in type"
                  :label="item.label"
                  :value="item.name">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-button @click.prevent="addData(0)">增加</el-button>
              <el-button @click.prevent="removeData(domain,0)" v-if="index!==0">删除</el-button>
            </el-col>
          </el-form-item>
        <h2>新建所需字段</h2>
        <el-form-item
          v-for="(domain, index) in formData.createData"
          :label="'新建字段' + index"
          label-width="80px"
        >
          <el-col :span="8">
            <el-select v-model="domain.name" clearable placeholder="请选择字段">
              <el-option
                v-for="item in tableData"
                :label="item.columnName"
                :value="item.columnName">
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="8">
            <el-select v-model="domain.type" clearable placeholder="请选择类型">
              <el-option
                v-for="item in type"
                :label="item.label"
                :value="item.name">
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="8">
            <el-button @click.prevent="addData(1)">增加</el-button>
            <el-button @click.prevent="removeData(domain,1)" v-if="index!==0">删除</el-button>
          </el-col>
        </el-form-item>
        <h2>表格显示字段</h2>
        <el-form-item label="表格显示字段">
          <el-select v-model="formData.tableData"  multiple placeholder="请选择表格显示字段" style="float: left;margin-left: 20px;width: 300px">
            <el-option
              v-for="item in tableData"
              :label="item.columnName"
              :value="item.columnName">
            </el-option>
          </el-select>
        </el-form-item>
        <h2>所含表单操作</h2>
        <el-form-item label="所含表单操作" >
          <el-select v-model="formData.dealData"  multiple placeholder="请选择表格显示字段" style="float: left;margin-left: 20px;width: 300px">
            <el-option label="编辑" value="edit"/>
            <el-option label="删除" value="delete"/>
            <el-option label="禁用" value="enable"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="createTemplate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {createTemplate, fetchTable, fetchTableInfo} from "../api/table";
import Pagination from '@/components/Pagination'
  import axios from 'axios'
export default {
  name: 'HelloWorld',
  components: {Pagination},
  data () {
    return {
      msg: '生成工具',
      listQuery: {
        page: 1,
        limit: 10,
        tableName: null,
      },
      list: null,
      total: 0,
      tableData:[],
      dialogFormVisible:false,
      type:[
        {
        id:1, name:"input",label:"输入框"
      },
        {
          id:2, name:"select",label:"下拉选"
        },
        {
          id:3, name:"radio",label:"单选框"
        },
      ],
      formData:{
        tableName:null,
        searchData:[{
          name:null,
          type:null
        }],
        createData:[{
          name:null,
          type:null
        }],
        tableData:[],
        dealData:[]
      }
    }
  },
  created:function(){
    this.getList()
  },
  methods:{
    createTemplate(){
      axios({ // 用axios发送post请求
        method: 'post',
        url: process.env.BASE_API+"/base/generator/create", // 请求地址
        data: this.formData, // 参数
        responseType: 'blob', // 表明返回服务器返回的数据类型
        headers:{
          'Content-Type': 'application/json; application/octet-stream'
        },
      })
        .then((res) => { // 处理返回的文件流
          this.dialogFormVisible = false
          const blob = new Blob([res.data]);
          const downloadElement = document.createElement("a");
          const href = window.URL.createObjectURL(blob);
          //后台再header中传文件名
          const name = "code.zip";
          downloadElement.href = href;
          downloadElement.download = name;
          document.body.appendChild(downloadElement);
          downloadElement.click();
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
    },

    addData(type) {
      switch (type) {
        case 0:
          this.formData.searchData.push({
            name:null,
            type:null
          });
          return;
        case 1:
          this.formData.createData.push({
            name:null,
            type:null
          });
          return;
      }

    },
    handleEdit(row){
      this.formData={
        tableName:row.tableName,
        searchData:[{
          name:null,
          type:null
        }],
        createData:[{
          name:null,
          type:null
        }],
        tableData:[],
        dealData:[]
      };
      const tableName={tableName:row.tableName};
      fetchTableInfo(tableName).then(response=>{
          this.tableData=response.data;
          this.dialogFormVisible=true;
      })
    },
    removeData(item,type) {
      switch (type) {
        case 0:
          var index = this.formData.searchData.indexOf(item)
          if (index !== -1) {
            this.formData.searchData.splice(index, 1)
          }
          return;
        case 1:
          var index = this.formData.createData.indexOf(item)
          if (index !== -1) {
            this.formData.createData.splice(index, 1)
          }
          return
      }

    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList(){
      fetchTable(this.listQuery).then(response => {
        let data=response.data.data;
        this.list=data.rows
        this.total=data.total
      })
    },
  }
}
</script>


